package cn.itcast.email;

import cn.itcast.utils.MailUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.util.Map;
public class EmailConsumer implements MessageListener {
    @Override
    public void onMessage(Message message) {
        try {
            //msg就是rabbitmq传来的消息，需要的同学自己打印看一眼

            byte[] body = message.getBody();//从mq中获取的是一个字节数组，内容就是生产者放入的map
            Map<String,String> map = JSON.parseObject(body, Map.class);//这是用的是fastjson转成map
            //获取内容
            String to = map.get("to");
            String subject = map.get("subject");
            String content = map.get("content");
            MailUtil.sendMsg(to , subject , content);//发送邮件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
